package com.lcz.mytest.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.lcz.mytest.R;
import com.lcz.mytest.db.UserService;
import com.lcz.mytest.fragment.CollectFragment;
import com.lcz.mytest.fragment.SHAllFragment;
import com.lcz.mytest.utils.SharedPrefsUtil;
import com.lcz.mytest.widget.ToolBarActivity;

public class LoginActivity extends ToolBarActivity {
    private EditText user;
    private EditText password;
    private CheckBox chb_remember = null;
    private boolean isRemember = false; // 判断是否为用户记住登录状态

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.login);
        getSupportActionBar().setTitle("登录");
        user = (EditText) findViewById(R.id.et_user);
        password = (EditText) findViewById(R.id.et_password);
        chb_remember = (CheckBox) findViewById(R.id.chb_remember);
        chb_remember.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (buttonView.isChecked()) {
                    isRemember = true;
                } else {
                    isRemember = false;
                }
            }
        });
    }

    /* 登录按钮监听事件 */
    public void login(View view) {
        final String name = user.getText().toString().trim();
        String pass = password.getText().toString().trim();

        UserService uService = new UserService(LoginActivity.this);
        if (!(TextUtils.isEmpty(name) && TextUtils.isEmpty(pass))) {
            boolean flag = uService.login(name, pass);
            if (flag) {

                new AlertDialog.Builder(LoginActivity.this)
                        .setTitle("提示")
                        .setMessage("登录成功")
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        if (isRemember) {
                                            SharedPrefsUtil.putValue(LoginActivity.this, "username", name);
                                            SharedPrefsUtil.putValue(LoginActivity.this, "isRemember", true);
                                        } else {
                                            SharedPrefsUtil.putValue(LoginActivity.this, "username", name);
                                            SharedPrefsUtil.putValue(LoginActivity.this, "isRemember", false);
                                        }
                                        //发送广播，刷新收藏页面
                                        Intent intent = new Intent();
                                        intent.setAction(CollectFragment.RECEIVER_INFILTER);
                                        intent.putExtra("login",true);
                                        sendBroadcast(intent);
                                        setResult(RESULT_OK, intent);
                                        finish();
                                    }
                                }).show().setCanceledOnTouchOutside(false);
            } else {
                new AlertDialog.Builder(LoginActivity.this).setTitle("提示")
                        .setMessage("帐号或密码错误").setPositiveButton("确定", null)
                        .show().setCanceledOnTouchOutside(false);
            }
        } else {
            new AlertDialog.Builder(LoginActivity.this).setTitle("提示")
                    .setMessage("帐号密码不能为空").setPositiveButton("确定", null)
                    .show().setCanceledOnTouchOutside(false);
        }
    }

    /* 注册按钮监听事件 */
    public void register(View view) {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), RegistersActivity.class);
        startActivityForResult(intent, 200);
    }

    /* 取消按钮监听事件 */
    public void cancle(View view) {
        finish();
    }
}
